package xjtu.se.articlecommunity.common.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.common.SMSService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import xjtu.se.articlecommunity.common.TencentCloudSMS;
import xjtu.se.articlecommunity.common.dao.SMSServiceDao;

import javax.annotation.Resource;
import java.util.Random;

@Service
@Transactional
public class SMSServiceImpl implements SMSService {

    @Resource
    private SMSServiceDao smsServiceDao;

    @Override
    public void sendSMS(String tel, String template, String[] templateParamSet, String[] phoneNumberSet) {
        try {
            TencentCloudSMS tencentCloudSMS = new TencentCloudSMS();
            tencentCloudSMS = smsServiceDao.getSMSTemplate(template);

            Credential cred = new Credential(tencentCloudSMS.getSecretId(), tencentCloudSMS.getSecretKey());

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);

            SendSmsRequest req = new SendSmsRequest();

            req.setSmsSdkAppId(tencentCloudSMS.getSdkAppId());
            req.setSignName(tencentCloudSMS.getSignName());
            req.setTemplateId(tencentCloudSMS.getTemplateId());
            req.setTemplateParamSet(templateParamSet);
            req.setPhoneNumberSet(phoneNumberSet);
            req.setSenderId("");
            req.setExtendCode("");
            req.setSessionContext("xxx");

            SendSmsResponse res = client.SendSms(req);
            System.out.println(SendSmsResponse.toJsonString(res));

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String sendVerificationCode(String tel) {
        String code = String.valueOf(new Random().nextInt(899999) + 100000);

        String[] phoneNumberSet = {"+86" + tel};
        String[] templateParamSet = {code, "5"};
        sendSMS(tel,"1",templateParamSet,phoneNumberSet);
        return code;
    }

    @Override
    public void sendRegistrationSuccessNotification(String tel, String u_id) {
        String[] phoneNumberSet = {"+86" + tel};
        String[] templateParamSet = {u_id};
        sendSMS(tel,"2",templateParamSet,phoneNumberSet);
    }
}
